from django.shortcuts import render,HttpResponse,redirect

# Create your views here.

def index(request):
	if request.method == 'GET':
		return render(request, 'index.html')
	elif request.method == 'POST':
		#==================由此看出获取单值用get，获取多个值用getlist，所以用get还是getlist取决于前端的选择=============
		name = request.POST.get('name')  #拿到前端模板的name值
		print(name)
		gender = request.POST.get('gender')  #拿到性别识别码1：男和2：女
		print(gender)
		aihao = request.POST.getlist('chbox') #0:羽毛球  1：打篮球   2：踢足球
		print(aihao)
		city = request.POST.getlist('city')   #获取城市识别码
		print(city)

		#==================文件上传，演示文件的方法=================
		fileobj = request.FILES.get('file')
		print(fileobj.name)   #输出文件名
		import os
		filepath = os.path.join('upload',fileobj.name)
		with open(filepath,'wb') as fn:   #要写入一个文件，当然先要打开一个文件
			for i in fileobj.chunks():   #上传文件，是分块上传到后端，所以我们python的后端也是一点点的读
				fn.write(i)
		return render(request, 'index.html')
	else:
		return redirect('/index')

def beam(request,**kwargs):
	print(kwargs['arg1'],kwargs['arg2'])
	return HttpResponse('参数1：%s   参数2：%s'  %(kwargs['arg1'],kwargs['arg2']))
